<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button
              v-if="isAuth('module:business:activation:code:form')"
              type="success"
              icon="Plus"
              @click="router.push({ name: 'module_business_activation_code_form' })">
              {{ i18n('business.activation.code.create') }}
            </el-button>

            <el-button
              v-if="isAuth('module:business:activation:code:export')"
              type="primary"
              icon="Download"
              @click="exportActivationCode">
              {{ i18n('common.export') }}
            </el-button>
          </div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button
              v-if="isAuth('module:business:activation:code:delete')"
              type="danger"
              icon="Delete"
              @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_select">
        <div class="input">
          <el-input v-model="dataForm.code" :placeholder="$t('common.please_input') + $t('business.activation.code.code')" clearable>
          </el-input>
        </div>
        <div class="input">
          <el-select
            v-model="dataForm.is_grant"
            :placeholder="i18n('common.please_select') + i18n('business.activation.code.is_grant')" filterable
            clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in statusList" :label="v.text" :key="k" :value="v.value">
            </el-option>
          </el-select>
        </div>
        <div class="input">
          <el-select
            v-model="dataForm.is_use"
            :placeholder="i18n('common.please_select') + i18n('business.activation.code.is_use')" filterable
            clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in statusList" :label="v.text" :key="k" :value="v.value">
            </el-option>
          </el-select>
        </div>
        <div class="date">
          <el-date-picker v-model="dataForm.create_time" type="daterange" :range-separator="$t('common.to')" :start-placeholder="$t('common.start_time')" :end-placeholder="$t('common.end_time')" clearable>
          </el-date-picker>
        </div>
        <div class="input">
          <el-button icon="Search" @click="getDataList(true)">
            {{ i18n('common.search') }}
          </el-button>
        </div>
      </div>
      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" :row-key="getRowKeys" stripe>
          <el-table-column type="selection" header-align="center" align="center" :reserve-selection="true">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column prop="code" :label="$t('business.activation.code.code')" min-width="150">
          </el-table-column>

          <el-table-column :label="$t('business.activation.code.face_value')" min-width="150">
            <template v-slot="scope">
              <el-text tag="b" type="success">
                {{ scope.row.face_value }} 天
              </el-text>
            </template>
          </el-table-column>

          <el-table-column :label="$t('business.activation.code.valid_time')" min-width="150">
            <template v-slot="scope">
              {{ scope.row.valid_time_text }}
            </template>
          </el-table-column>

          <el-table-column :label="i18n('business.activation.code.is_grant')" width="100">
            <template v-slot="scope">
              {{ scope.row.is_grant.text }}
            </template>
          </el-table-column>

          <el-table-column :label="i18n('business.activation.code.is_use')" width="100">
            <template v-slot="scope">
              {{ scope.row.is_use.text }}
            </template>
          </el-table-column>

          <el-table-column :label="i18n('business.activation.code.generator')" width="100">
            <template v-slot="scope">
              <el-text v-if="scope.row.user" type="primary">
                {{ scope.row.user.nickname }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column prop="create_time" :label="i18n('common.create_time')" width="160">
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="100">
            <template v-slot="scope">
              <el-button
                v-if="isAuth('module:business:activation:code:delete')"
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="activation-code-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/activation/code')

  const statusList = ref([])

  const dataForm = reactive([
    'is_grant',
    'is_use',
    'code',
    'create_time',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    dataListSelections,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const loadStatusList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'data_whether_status',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        statusList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const getRowKeys = (row) => {
    return row.id
  }

  const exportActivationCode = () => {
    $http({
      url: $http.adornUrl(`/business/activation/code/export`),
      method: 'post',
      data: $http.adornData({
        id: dataListSelections.value.map((item) => {return item.id}),
        is_grant: dataForm.is_grant,
        is_use: dataForm.is_use,
        code: dataForm.code,
        create_time: dataForm.create_time
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        window.open(data.data)
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    getDataList(model)

    loadStatusList()
  })
</script>
